package com.petcommunity.dao;

import com.petcommunity.model.Adoption;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdoptionDAO {

    public void addAdoption(Adoption adoption) {
        String sql = "INSERT INTO adoptions (pet_id, adopter_name, adopter_phone, adoption_date) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, adoption.getPetId());
            stmt.setString(2, adoption.getAdopterName());
            stmt.setString(3, adoption.getAdopterPhone());
            stmt.setDate(4, new java.sql.Date(adoption.getAdoptionDate().getTime()));
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Adoption> getAllAdoptions() {
        List<Adoption> list = new ArrayList<>();
        String sql = "SELECT * FROM adoptions ORDER BY adoption_id ASC ";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Adoption adoption = new Adoption();
                adoption.setAdoptionId(rs.getInt("adoption_id"));
                adoption.setPetId(rs.getInt("pet_id"));
                adoption.setAdopterName(rs.getString("adopter_name"));
                adoption.setAdopterPhone(rs.getString("adopter_phone"));
                adoption.setAdoptionDate(rs.getDate("adoption_date"));
                list.add(adoption);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Adoption getAdoptionById(int id) {
        Adoption adoption = null;
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM adoptions WHERE adoption_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                adoption = new Adoption();
                adoption.setAdoptionId(rs.getInt("adoption_id"));
                adoption.setPetId(rs.getInt("pet_id"));
                adoption.setAdopterName(rs.getString("adopter_name"));
                adoption.setAdopterPhone(rs.getString("adopter_phone"));
                adoption.setAdoptionDate(rs.getDate("adoption_date"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return adoption;
    }

    public void updateAdoption(Adoption adoption) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE adoptions SET pet_id=?, adopter_name=?,adopter_phone = ?,adoption_date=? WHERE adoption_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, adoption.getPetId());
            ps.setString(2, adoption.getAdopterName());
            ps.setString(3, adoption.getAdopterPhone());
            ps.setDate(4, adoption.getAdoptionDate());
            ps.setInt(5, adoption.getAdoptionId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteAdoption(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM adoptions WHERE adoption_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
